package com.UnionPayCenter.operation.manager.impl;

import com.UnionPayCenter.mapper.ActivityConfigMapper;
import com.UnionPayCenter.operation.entity.ActivityConfig;
import com.UnionPayCenter.operation.enums.ActivityOperationTypeEnum;
import com.UnionPayCenter.operation.manager.ActivityConfigManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ActivityConfigManagerImpl implements ActivityConfigManager {

    @Autowired
    private ActivityConfigMapper activityConfigMapper;

    @Override
    public Integer addActivityConfig(ActivityConfig activityConfig) {

        if (activityConfig.getMemberCardDiscount() == null) {
            activityConfig.setMemberCardDiscount(1);
        }
        if (activityConfig.getCoupon() == null) {
            activityConfig.setCoupon(1);
        }
        ActivityConfig dbConfig = getByShopId(activityConfig.getShopId(), activityConfig.getActivityOperationType());
        if (dbConfig != null) {
            dbConfig.setCoupon(activityConfig.getCoupon());
            dbConfig.setMemberCardDiscount(activityConfig.getMemberCardDiscount());
            updateActivityConfig(dbConfig);
            return dbConfig.getActivityConfigId();
        } else {
            activityConfigMapper.insert(activityConfig);
            return activityConfig.getActivityConfigId();
        }
    }

    @Override
    public void updateActivityConfig(ActivityConfig activityConfig) {
        activityConfigMapper.updateByPrimaryKeySelective(activityConfig);
    }

    @Override
    public List<ActivityConfig> getByShopId(Integer shopId) {
        return activityConfigMapper.selectByShopId(shopId);
    }

    @Override
    public ActivityConfig getByShopId(Integer shopId, ActivityOperationTypeEnum operationTypeEnum) {
        return activityConfigMapper.selectByShopIdAndOperateType(shopId, operationTypeEnum);
    }

}
